package com.yt.base.controller.app;

import com.google.common.base.Strings;
import com.yt.app.core.interfaces.LibraryService;
import com.yt.app.core.model.Library;
import com.yt.app.core.model.Teacher;
import com.yt.base.controller.core.BaseController;
import com.yt.core.MsgBean;
import com.yt.core.dao.PagingBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.rmi.MarshalledObject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/app/library")
public class LibraryController extends BaseController{
    @Autowired
    private LibraryService libraryService;
    @RequestMapping("list")
    public ModelAndView list(@RequestParam(value = "start", required = false) String start,
                             @RequestParam(value = "limit", required = false) String limit){
        ModelAndView mav=new ModelAndView("app/library/list");
        QueryFilter filter=new DynamicSQLFilterImpl();
        filter.setSortField("id",true);
        PagingBean page = new PagingBean();
        int currentPage = 0;
        int pageSize = 3;
        if (StringUtils.isNotEmpty(limit) && StringUtils.isNumeric(limit)) {
            pageSize = Integer.parseInt(limit);
        }
        if (StringUtils.isNotEmpty(start) && StringUtils.isNumeric(start)) {
            currentPage = (Integer.parseInt(start) - 1) * pageSize;
        }
        page.setPageSize(pageSize);
        page.setStart(currentPage);
        List<Library> list=libraryService.find(filter);
        int total=libraryService.count(filter);
        mav.addObject("list",list);
        mav.addObject("total",total);
        return mav;

    }
    @RequestMapping("toAdd")
    public ModelAndView toAdd(){
        ModelAndView mav=new ModelAndView("app/library/add");
        return mav;
    }
    @RequestMapping("toEdit")
    public ModelAndView toEdit(@RequestParam(value = "id", required = true) Long id){
        ModelAndView mav=new ModelAndView("app/library/add");
        Library library=libraryService.get(id);
        mav.addObject("obj",library);
        return mav;
    }
    @RequestMapping("save")
    public
    @ResponseBody
    Object save(@RequestParam(value = "id", required = false) Long id,
                @RequestParam(value = "name", required = false) String name,
                @RequestParam(value = "author", required = false) String  author,
                @RequestParam(value = "type", required = false) String type,
                @RequestParam(value = "press",required = false) String press,
                @RequestParam(value = "dateText",required = false)String date

    ) {
        MsgBean msg = new MsgBean();
        try {
            Library library = null;
            if (library == null) {
                library = new Library();
                library.setId(id);
                library.setName(name);
                library.setAuthor(author);
                library.setPress(press);
                library.setType(type);
            }
            if (id !=null && id > 0) {
                library.setName(name);
                library.setAuthor(author);
                library.setPress(press);
                library.setType(type);
            }
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date dateText = dateFormat.parse(date);
            library.setDate(dateText.getTime() / 1000);
            if(library.getId()!=null&&library.getId()>0){
                libraryService.update(library);
            }else {
                libraryService.insert(library);
            }
        }catch (Exception e){
            msg.failure("操作失败，请检查参数是否合理！");
        }
        return msg.returnMsg();
    }
    @RequestMapping("/multiDel")
    @ResponseBody
    public Object multiDel(@RequestParam("ids") String ids) {
        MsgBean msg = new MsgBean();
        String[] idArr = ids.split(",");
        if (ids != null) {
            for (String id : idArr) {
                libraryService.remove(Long.valueOf(id));
            }
        }
        return msg.returnMsg();
    }
}
